package e.odbo.data.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ClassUtils {
    private final static Logger logger= LoggerFactory.getLogger(ClassUtils.class);

    public static String getPackageBaseClassName(Class fromClass){
        String typeName=fromClass.getName();
        //logger.info("inClassName:"+fromClass.getName());
        typeName=typeName.substring(typeName.lastIndexOf(".")+1);
        //logger.info("tancClassName:"+typeName);
        if(typeName.lastIndexOf('$')>0)
            typeName=getInnerClassName(typeName);
        return typeName;
    }

    private static String getInnerClassName(String typeName){
            logger.debug("getInnerClassName:{}",typeName);
            String baseClassName=typeName.substring(0,typeName.lastIndexOf('$'));
            int trimLength=baseClassName.length()*2+1;
            //对匿名内部类过滤InnerClass$4
            if(trimLength<typeName.length()) {
                typeName = typeName.substring(trimLength);
                typeName = baseClassName + "." + typeName;
            }else{
                typeName=typeName.replace('$','.');
            }
        return typeName;
    }

    /*public static String getPackageBaseImportClassName(Class fromClass){
        String typeName=fromClass.getName();
        if(typeName.lastIndexOf('$')>0)
            typeName=typeName.substring(0,typeName.lastIndexOf("."));

       return typeName;
    }*/

    public static String getImportClassName(Class fromClass){
        String typeName=fromClass.getName();
        if(typeName.lastIndexOf('$')>0) {
            typeName = typeName.substring(0, typeName.lastIndexOf(".")) + "." + getPackageBaseClassName(fromClass);
            typeName=typeName.substring(0,typeName.lastIndexOf("."));
        }
        return typeName;
    }

    public static String getClassName(String nameString){
        nameString=nameString.toLowerCase();
        nameString=StringUtils.underlineToCamel(nameString);
        return StringUtils.upStartChar(nameString,true);
    }

    public static String getFieldName(String nameString){
        nameString=nameString.toLowerCase();
        nameString=StringUtils.underlineToCamel(nameString);
        return StringUtils.upStartChar(nameString,false);
    }

    public static String getFieldMethodGeter(String nameString){
        nameString=getFieldName(nameString);
        return "get"+StringUtils.upStartChar(nameString,true);
    }

    public static String getFieldMethodSeter(String nameString){
        nameString=getFieldName(nameString);
        return "set"+StringUtils.upStartChar(nameString,true);
    }

    public static String getI18NString(String... nameStrings){
        return StringUtils.join(nameStrings,'_');
    }



}
